<?php
namespace app\common\model;

/**
* Category模型
*/
class Category extends BaseModel
{
	//tp5自带属性，自动赋值字段‘create_time’,'update_time'
	// protected $autoWriteTimestamp = true;

    //tp5.07自带属性，格式化字段类型输出
	// protected $type = [
 //        'create_time'     =>  'timestamp:Y/m/d h:i',
 //        'update_time'  =>  'timestamp:Y/m/d h:i',
 //    ];

	public function add($data){
		$data['status'] = 1;
		// $data['create_time'] = time();
		return $this->save($data);
	}

	/**
	*根据状态条件获取一级栏目
	*$parent_id 父级ID
	*$tatus数组
	*/
	public function getCategorysByParentId($parent_id = 0, $status = ['eq',1], $isPage = 0, $limit = 0){
		$data = [
			'status' => $status,
			'parent_id' => $parent_id,
		];

		$order = [
			'listorder' => 'desc',
			'id' => 'desc',
		];

		if($isPage){
			$result = $this->where($data)
				->order($order)
				->paginate();
		}else if($limit){
			$result = $this->where($data)
				->order($order)
				->limit($limit)
				->select();
		}else{
			$result = $this->where($data)
				->order($order)
				->select();
		}

		// echo $this->getLastSql();
		return $result;
	}
}